import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/views/layout/index.vue'
import store from '@/store'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [{
    path: '/login',
    component: () =>
      import('@/views/login/index.vue')
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      component: () =>
        import('@/views/dashboard/index.vue')
    },
    {
      path: 'article',
      component: () =>
        import('@/views/article/index.vue')
    }
    ]
  }
  ]
})
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  // 1. 获取token
  const token = store.state.user.token
  // 2. 判断
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
